package _09_heap;

import _09_heap.test.MinHeap;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author: mornd
 * @dateTime: 2023/6/10 - 15:07
 */
public class MyTest {
    @Test
    public void test() {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
        IntMaxHeap maxHeap = new IntMaxHeap(arr);
        System.out.println(maxHeap); // [7, 5, 6, 4, 2, 1, 3]
//        System.out.println(maxHeap.poll()); // [6, 5, 3, 4, 2, 1]
//        System.out.println(maxHeap.poll(1));

//        maxHeap.replace(3, 5);

//        maxHeap.offer(9);
        maxHeap.asc();
        System.out.println("asc:" + maxHeap);
        maxHeap.desc();
        System.out.println("desc:" + maxHeap);
    }


    /*
        leetcode 找出数组中第k大的元素
        [1,2,3,3,4]  k = 4
        结果：2
        大顶堆实现
     */
    @Test
    public void test2() {
        int k = 4;
        IntMaxHeap heap = new IntMaxHeap(new int[]{1, 2, 3, 3, 4});
        int v = 0;
        while (k-- > 0) {
            v = heap.poll();
        }
        System.out.println(v);
    }

    /*
        小顶堆
     */
    @Test
    public void testMin() {
        IntMinHeap intMinHeap = new IntMinHeap(new int[]{7, 6, 5, 4, 3, 2, 1});
        System.out.println(intMinHeap);
    }


    /*
        leetcode 题：求出数据流的中位数
     */
    @Test
    public void testLeetCodeFindMiddle() {
        MedianFinder finder = new MedianFinder();
        finder.addNum(1);
        finder.addNum(4);
        finder.addNum(5);
        finder.addNum(8);
        System.out.println(finder);
        finder.addNum(2);
        finder.addNum(9);
        System.out.println(finder);
        System.out.println(finder.findMedian());
    }

    @Test
    public void testJDK() {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.offer(1);
        queue.offer(3);
        queue.offer(2);
        System.out.println(queue);

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());

        int compare = Integer.compare(1, 2);
    }
}
